import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:visibility_detector/visibility_detector.dart';


///  视频播放view
class VideoNetView extends StatefulWidget {
  final VideoPlayerController videoPlayerController;

  const VideoNetView({
    super.key,
    required this.videoPlayerController,
  });

  @override
  State<VideoNetView> createState() => _VideoNetViewState();
}

class _VideoNetViewState extends State<VideoNetView>
    with AutomaticKeepAliveClientMixin {
  @override
  void dispose() {
    super.dispose();
    widget.videoPlayerController.pause();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return VisibilityDetector(
        key: Key("kk"),
        onVisibilityChanged: (visibilityInfo) {
          var visiblePercentage = visibilityInfo.visibleFraction * 100;
          print(visiblePercentage);
          if (visiblePercentage > 0) {
            //页面可见
            if (visiblePercentage == 100) {
              // widget.videoPlayerController.play();
              //print("开始播放");
            }
          } else {
            //页面不可见
            //widget.videoPlayerController.pause();
            // print("停止播放");
          }
        },
        child: Container(
          color: Colors.black,
          child: Center(
            child: AspectRatio(
              aspectRatio: widget.videoPlayerController.value.aspectRatio,
              child: SizedBox(
                width: MediaQuery.of(context).size.width,
                child: VideoPlayer(widget.videoPlayerController),
              ),
            ),
          ),
        ));
  }

  @override
  bool get wantKeepAlive => true;
}
